<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>service服务的定义及用法</title>
    <link rel="stylesheet" href="./libs/css/bootstrap.css">
</head>
<body ng-app='app'>
    <div class="container">
        <div ng-controller="demoCtrl" class="well well-info">
            <p>{{date}}</p>
            <p ng-bind="dateF"></p>
        </div>
    </div>
</body>
<script src="./libs/js/angular-1.6.9.min.js"></script>
<script>
    var app=angular.module('app',[]);

    // 构造器的写法
    app.service('tools',['$rootScope',function($rootScope){
        this.dateFmt=function(input,fmt='yyyy/mm/dd'){
            var output;
            // input=new Date(input);
            var year=input.getFullYear();
            var month=input.getMonth()+1;
            var day=new Date().getDate();
            if(fmt=='yyyy/mm/dd'){
                output=`${year}/${month}/${day}`;
            }else{
                var hours=input.getHours();
                var minutes=input.getMinutes();
                var seconds=input.getSeconds();
                output=`${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
            }
            return output;
        }

        this.strRev=function(){

        }
    }]);

    //普通函数的写法
    // app.factory('tools',['$rootScope',function($rootScope){
    //     return {
    //         dateFmt:function(input,fmt='yyyy/mm/dd'){
    //             var output;
    //             // input=new Date(input);
    //             var year=input.getFullYear();
    //             var month=input.getMonth()+1;
    //             var day=new Date().getDate();
    //             if(fmt=='yyyy/mm/dd'){
    //                 output=`${year}/${month}/${day}`;
    //             }else{
    //                 var hours=input.getHours();
    //                 var minutes=input.getMinutes();
    //                 var seconds=input.getSeconds();
    //                 output=`${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
    //             }
    //             return output;
    //         }
    //     }
    // }]);
    // 将服务tools注入到demoCtrl控制其中
    app.controller('demoCtrl',['$scope','tools',function($scope,tools){
        $scope.date=new Date();
        $scope.dateF=tools.dateFmt($scope.date,'');
    }]);
</script>
</html>